package com.woniu.smart.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.smart.dto.ContractDTO;
import com.woniu.smart.dto.ContractInformationDTO;
import com.woniu.smart.entity.Contract;
import com.woniu.smart.service.ContractService;
import com.woniu.smart.util.Result;
import com.woniu.smart.vo.ContractVo;
import com.woniu.smart.vo.ContractsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: smart
 * @description: 对合同的操作
 * @author: meibing
 * @create: 2022-10-20 14:12
 **/

@RestController
@RequestMapping("contracts")
public class ContractController {

    @Autowired
    private ContractService contractService;

    /**
     * 查询全部合同信息
     * @return list集合
     */
    @GetMapping("")
    public Result selectAll(){
        List<Contract> contractList = contractService.selectContractAll();
        return Result.ok().data("contractList",contractList);
    }


    /**
     * 查询合同的部分信息用于前端展示
     * @return list集合
     */
    @PostMapping("selectAlls")
    public Result selectAllS(@RequestBody ContractDTO contractDTO){
        PageHelper.startPage(contractDTO.getCurrentPage(),contractDTO.getPageSize());
        List<ContractsVO> contractList = contractService.selectContractAllS(contractDTO);
        PageInfo<ContractsVO> contractsVOPageInfo = new PageInfo<>(contractList);
        return Result.ok().data("contractList",contractList).data("total",contractsVOPageInfo.getTotal());
    }


//    /**
//     * 查询合同中企业为空的合同信息
//     * @return list集合
//     */
////    @GetMapping("selectCOntraByEnterIsNull")
////    public Result selectAllByEnter(@RequestBody ContractDTO contractDTO){
////        List<ContractsVO> contractList = contractService.selectContractAllS(contractDTO);
////        return Result.ok().data("contractList",contractList);
////    }




    /**
     * 根据ContractVo对象筛选查询合同信息
     *
     * @author meibing
     * @date 2022/10/20 21:40
     * @param contractvo
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("")
    public Result queryContractByContractVo(@RequestBody ContractVo contractvo){
        List<Contract> contractlist = contractService.queryContractByContractVo(contractvo);
        return Result.ok().data("contractList",contractlist);
    }



    /**
     * 根据id查询合同信息
     *
     * @author meibing
     * @date 2022/10/20 21:40
     * @param id 主键id
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("{id}")
    public Result queryByContractId(@PathVariable("id") Long id){
        Contract contract = contractService.selectByPrimaryKey(id);
        return Result.ok().data("building",contract);
    }

    /**
     * 新增合同
     * @author meibing
     * @date
     * @param  contract
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("insertContract")
    public Result insertContract(@RequestBody Contract contract){
        //先将合同信息新增至合同表内，返回合同id
        int i = contractService.insertSelective(contract);
        if(i>0){
            return Result.ok().data("return",i);
        }else{
            return Result.error("新增合同失败");
        }
    }


    /**
     * 新增合同的同时新增账单信息表。
     * @author meibing
     * @date    模板对象
     * @param  contractInformationDTO
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("insertContractInter")
    public Result insertContract(@RequestBody ContractInformationDTO contractInformationDTO){
        int i = contractService.insertSelectives(contractInformationDTO);
        if(i>0){
            return Result.ok().data("return",i);
        }else{
            return Result.error("新增合同失败");
        }
    }

    /**
     * 删除合同根据list
     * @author meibing
     * @date
     * @param  eids
     * @return com.woniu.smart.util.Result
     **/
    @DeleteMapping("deleteList")
    public Result deleteAllToContractId(@RequestParam("eids") List<Integer> eids){
        int i = contractService.deleteAll(eids);
        if (i>0){
            return Result.ok();
        }else {
            return Result.error();
        }

    }




    /**
     * 新增合同，企业，企业员工，账单，修改房间状态
     * @param contract
     * @return com.woniu.smart.util.Result
     * @author meibing
     * @date
     **/
    @PostMapping("insertDTO")
    public Result insertSelectives(@RequestBody ContractInformationDTO contract) {
        int i = contractService.insertSelectives(contract);
        if (i > 0) {
            return Result.ok().data("return", i);
        } else {
            return Result.error("新增租户企业表失败");
        }
    }


}
